import * as api from "../../api/address";

Component({
    options:{
        styleIsolation:'isolated'
    },
    data:{
        multiArray:[[],[],[]],
        multiIndex: [0, 0, 0],
        optionRange:[[],[],[]],
        optionIndex:[0,0,0]
    },
    properties:{
        pickerId:{
            type:Array,
            value:[],
            observer: function (val) {
                this.init( val );
            }
        }
    },
    // lifetimes: {
    //     attached(){
           
    //     }
    // },
    
    methods: {
        init( areaArray ){

            if(areaArray[2]){

                let province, provinceIndex, city, cityIndex;

                api.findChildAddress({
                    parentCode:''
                })
                .then((res)=>{
                    provinceIndex = res.findIndex((el)=>{
                        return el.regionCode == areaArray[0];
                    });
                    province = res;
                    return api.findChildAddress({
                        parentCode:areaArray[0]
                    })
                })
                .then((res)=>{
                    if(res && res.length > 0){
                        cityIndex = res.findIndex((el)=>{
                            return el.regionCode == areaArray[1];
                        });
                        city = res;
                        return api.findChildAddress({
                            parentCode:areaArray[1]
                        })
                    }
                })
                .then((res)=>{
                    if(res && res.length > 0){
                        let index = res.findIndex((el)=>{
                            return el.regionCode == areaArray[2];
                        });
                        this.setData({
                            [`multiArray[0]`]:province,
                            [`multiIndex[0]`]:provinceIndex,
                            [`multiArray[1]`]:city,
                            [`multiIndex[1]`]:cityIndex,
                            [`multiArray[2]`]:res,
                            [`multiIndex[2]`]:index,
                            "optionRange":[province, city, res]
                        })
                    }
                })
            }
            else{

                let province=[], city=[], country=[];

                api.findChildAddress({
                    parentCode:""
                })
                .then((res)=>{
                    province = res;
                    return api.findChildAddress({
                        parentCode:province[0].regionCode
                    })
                })
                .then((res)=>{
                    city = res;
                    return api.findChildAddress({
                        parentCode:city[0].regionCode
                    })
                })
                .then((res)=>{
                    country = res;

                    this.setData({
                        optionRange:[province, city, country]
                    })
                })
            }

            // if(areaArray[0]){
            //     api.findChildAddress({
            //         parentCode:'100000000000'
            //     })
            //     .then((res)=>{
            //         let index = res.findIndex((el)=>{
            //             return el.regionCode == areaArray[0];
            //         });

            //         this.setData({
            //             [`multiArray[0]`]:res,
            //             [`multiIndex[0]`]:index
            //         });

            //         console.log( this.data.multiArray )
            //     })
            // }

            // if(areaArray[1]){
            //     api.findChildAddress({
            //         parentCode:areaArray[0]
            //     })
            //     .then((res)=>{
            //         if(res && res.length > 0){
            //             let index = res.findIndex((el)=>{
            //                 return el.regionCode == areaArray[1];
            //             });
            //             this.setData({
            //                 [`multiArray[1]`]:res,
            //                 [`multiIndex[1]`]:index
            //             })
            //         }
            //     })
            // }

            // if(areaArray[2]){
            //     api.findChildAddress({
            //         parentCode:areaArray[1]
            //     })
            //     .then((res)=>{
            //         if(res && res.length > 0){
            //             let index = res.findIndex((el)=>{
            //                 return el.regionCode == areaArray[2];
            //             });
            //             this.setData({
            //                 [`multiArray[2]`]:res,
            //                 [`multiIndex[2]`]:index
            //             })
            //         }
            //     })
            // }
        },
        async bindColumnChange(e){

            let { column, value } = e.detail;
            let provinceIndex = this.data.optionIndex[0];
            let cityIndex = this.data.optionIndex[1];
            let countryIndex = this.data.optionIndex[2];
            let province = [], city = [], country = [];

            if( column == 0 && provinceIndex != value ){

                let parentCode = this.data.optionRange[0][value].regionCode;
                let city = await api.findChildAddress({
                    parentCode:parentCode
                });
                let country = await api.findChildAddress({
                    parentCode:city[0].regionCode
                });

                this.setData({
                    [`optionRange[1]`]:city,
                    [`optionRange[2]`]:country,
                    optionIndex:[value, 0, 0],
                });

            }
            else if(column == 1 && city != value){
                let parentCode = this.data.optionRange[1][value].regionCode;
                api.findChildAddress({
                    parentCode:parentCode
                })
                .then((res)=>{
                    this.setData({
                        [`optionRange[2]`]:res,
                        [`optionIndex[1]`]:value,
                        [`optionIndex[2]`]:5,
                    })
                })
            }
        },
        bindPickerChange(e){

            //console.log( this.data.optionRange )

            let { value } = e.detail;
            this.setData({
                multiArray:this.data.optionRange,
                multiIndex:value
            });

            let [provinceIndex, cityIndex, countryIndex] = value;
            let [province, city, country] = this.data.optionRange;

            let obj = {
                provinceName:province[provinceIndex].regionName,
                provinceCode:province[provinceIndex].regionCode,
                cityName:city[cityIndex].regionName,
                cityCode:city[cityIndex].regionCode,
                countryName:country[countryIndex].regionName,
                countryCode:country[countryIndex].regionCode
            }

            this.triggerEvent('confirm', obj)
        }
    },
})
